using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;

/// <summary>
/// Summary description for OrderDetailsDAO
/// </summary>
public class OrderDetailsDAO : DBConnection
{
    #region Sql_command
    string SQL_SELECT_ALL = "";
    string SQL_SELECT_ID = "";
    string SQL_INSERT = "insert into OrderDetails(OrderID, ItemID, ItemTypeID,  Price, Quantity) values(@orderId, @itemId, @itemTypeId, @price, @quantity)";
    string SQL_UPDATE = "update OrderDetails set OrderID = @orderId, ItemID = @itemId, ItemTypeID = @itemTypeId, Price = @price, Quantity = @quantity where OrderDetailsID = @orderDetailsId";
    string SQL_DELETE = "delete from OrderDetails where OrderDetailsID = @orderDetailsId";

    #endregion

    #region fields
    string message = "";
    #endregion

    #region properties
    public string Message
    {
        get { return message; }       
    }
    #endregion

    #region constructors
    public OrderDetailsDAO()
    {

    }
    #endregion

    #region methods
    public int InsertOrderDetails(Item ord)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_INSERT, conn);
            cmd.Parameters.AddWithValue("@orderId", ord.OrderID);
            cmd.Parameters.AddWithValue("@itemId", ord.ID);
            cmd.Parameters.AddWithValue("@itemTypeId", ord.TypeID);           
            cmd.Parameters.AddWithValue("@price", ord.Price);
            cmd.Parameters.AddWithValue("@quantity", ord.Quantity);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }

    public int UpdateOrderDetails(Item ord)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_UPDATE, conn);
            cmd.Parameters.AddWithValue("@orderDetailsId", ord.ID);
            cmd.Parameters.AddWithValue("@orderId", ord.OrderID);
            cmd.Parameters.AddWithValue("@itemId", ord.ID);
            cmd.Parameters.AddWithValue("@itemTypeId", ord.TypeID);         
            cmd.Parameters.AddWithValue("@price", ord.Price);
            cmd.Parameters.AddWithValue("@quantity", ord.Quantity);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }

    public int DeleteOrderDetails(Item ord)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_INSERT, conn);
            cmd.Parameters.AddWithValue("@orderDetailsId", ord.ID);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }
    #endregion



}
